
package vectors;

import java.util.*;
import java.io.*;

public class JLinkedList implements VectorIn,Serializable {
   private LinkedList<Double> lList;
   private ArrayList observer;
public JLinkedList(){
    lList = new LinkedList<Double>();
    observer = new ArrayList();
}
public JLinkedList(int length){
    for(int i =0;i < length;i++){
        lList = new LinkedList<Double>();
        lList.set(i, 0.0);
        
    }
}
public JLinkedList(Double ... e) {
        for (Double elem : e) {
            lList.add(elem);
        observer = new ArrayList();
        }
    }
    @Override
    public void setElement(double value, int index) {
     lList.set(index, value);
    elementNotifyObservers(index);
    }
    public void add(double value) {     
     lList.add(value);
    updateNotifyObservers();
    }
    @Override
    public Double getElement(int index) {
    return lList.get(index);
    }
    @Override
    public int getSize() {
    return lList.size();
    }

    @Override
    public void massiveToVector(double[] massive) {
     for(int i = 0; i < massive.length;i++){
        lList.set(i,massive[i]);
     updateNotifyObservers();   
     }
    }
    @Override
    public void objectToVector(VectorIn Vector1) {
       for(int i = 0; i < Vector1.getSize(); i++){
        lList.set(i,Vector1.getElement(i));
    updateNotifyObservers();
       }
}
    @Override
    public void multiplyVector(double multiply) {
     for(int i = 0; i < lList.size();i++){
         lList.set(i,lList.get(i) * multiply);
     updateNotifyObservers();
     }
    }

    @Override
    public void vectorPlusVector(VectorIn Vector) {
    if(lList.size() != Vector.getSize()){
        System.out.print("Ошибка!Задайте другой размер.");
    }else{
         for(int i = 0; i < lList.size() && i <Vector.getSize(); i++){
           lList.set(i,lList.get(i)+Vector.getElement(i));
     updateNotifyObservers();
        }
   }
}
    @Override
 public boolean equalsVectors(VectorIn Vector) {
    boolean f;
    if (Vector.getSize() != lList.size())
          return false;
      for (int i = 0; i < lList.size(); i++){
          if(lList.get(i) != Vector.getElement(i))
          return false;
}
      return true;
    }
   @Override
public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if(!(obj instanceof VectorIn))
        return false;
            VectorIn vec = (VectorIn) obj; 
        if (this.lList.size() != vec.getSize())
            return false;

        for (int i = 0; i < this.lList.size(); i++) {
            if(getElement(i) != vec.getElement(i))
                return false;
        }

        return true;
} 
   @Override
 public Object clone() throws CloneNotSupportedException{
        JLinkedList result;
        result = (JLinkedList) super.clone();
        result.lList = (LinkedList) lList.clone();
    return result;
}    
   @Override
public int hashCode(){
    int result ;
    result = lList.hashCode();
    return result;
}
public String toString(){
   lList.toString();
    return lList.toString();
}   
public Iterator<Double> iterator() {
        return new Iterator<Double>() {
            public int elem = 0;

            @Override
            public void remove() {
            }
            @Override
            public Double next() {
                return lList.get(elem++);
            }
            @Override
            public boolean hasNext() {
                if (elem < lList.size())
                    return true;
                else
                    return false;
            }
        };
    }
@Override
    public void registerObserver(Observer o) {
          observer.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int i = observer.indexOf(o);
        if (i>=0) 
            observer.remove(i);
    }

    @Override
    public void elementNotifyObservers(int index) {
        for ( int i =0; i< observer.size(); i++)
        {
           Observer observers = (Observer) observer.get(i); 
            observers.updateElement(index);
    }
 }
     
     public void updateNotifyObservers() {
        for ( int i =0; i< observer.size(); i++)
        {
           Observer observers = (Observer) observer.get(i); 
            observers.update();
    }
 }

}

